import { getFoods, searchFoods } from "../../service/food"
import { getCategorys } from "../../service/category"
import { getShopDetail } from "../../service/shop"

// pages/home/home.ts
Page({

  /**
   * 页面的初始数据
   */
  data: {
    activeKey: 0,
    shop: {
      name: '',
      img: ''
    },
    categorys: [],
    foods: [],
    showFoods: [],
    cart: [],
  },
  onLoad() {
    this.loadShopDetail()
    this.loadCategorys().then(() => {
      this.loadFoods().then(() => {
        this.loadShowFoods()
      })
    })
  },
  onShow() {
    const app = getApp()
    if (app.globalData.isCleanCart) {
      const foods = this.data.foods
      foods.forEach((item: any) => {
        item.count = 0
      })
      const showFoods = this.data.showFoods
      showFoods.forEach((item: any) => {
        item.count = 0
      })
      this.setData({
        cart: [],
        foods,
        showFoods
      })
      app.globalData.isCleanCart = false
    }
  },
  async loadShopDetail()  {
    const res = await getShopDetail()
    this.setData({
      shop: {
        name: res.data.shop.name,
        img: res.data.shop.img
      }
    })
  },
  async loadCategorys() {
    const res = await getCategorys()
    const categorys = res.data.categorys
    categorys.sort((a: any, b: any) => a.grade - b.grade)
    this.setData({
      categorys
    })
  },
  async loadFoods() {
    const res = await getFoods()
    const foods = res.data.foods.map((item: any) => {
      item.count = 0
      return item
    })
    this.setData({
      foods
    })
  },
  onCategoryChange(event: any) {
    this.setData({
      activeKey: event.detail
    })
    this.loadShowFoods()
  },
  loadShowFoods() {
    const category = this.data.categorys[this.data.activeKey] as any
    const showFoods = this.data.foods.filter((item: any) => item.categoryId === category.id)
    this.setData({
      showFoods
    })
  },
  changeCart(food: any) {
    const hasFood = this.data.cart.find((item: any) => item.id === food.id) as any
    const cart = this.data.cart
    if (!hasFood) {
      cart.push(food as never)
      this.setData({
        cart
      })
    } else if (food.count === 0) {
      const foodIndex = this.data.cart.findIndex((item: any) => item.id === food.id)
      cart.splice(foodIndex, 1)
      this.setData({
        cart
      })
    } else {
      const foodIndex = this.data.cart.findIndex((item: any) => item.id === food.id)
      cart.splice(foodIndex, 1)
      cart.splice(foodIndex, 0, food as never)
      this.setData({
        cart
      })
    }
  },
  onFoodChange(e: any) {
    const food = e.detail
    const foodIndex = this.data.foods.findIndex((item: any) => item.id === food.id)
    const foods = this.data.foods
    foods.splice(foodIndex, 1, food as never)
    this.setData({
      foods
    })
    this.loadShowFoods()
    this.changeCart(food)
  },
  onCartChange(e: any) {
    const food = e.detail
    const foodIndex = this.data.foods.findIndex((item: any) => item.id === food.id)
    const foods = this.data.foods
    foods.splice(foodIndex, 1, food as never)
    this.setData({
      foods
    })
    this.loadShowFoods()
    this.changeCart(food)
  },
 async onSearch() {
    wx.navigateTo({
      url: '/pages/search/search'
    })
  }
})